package gdepot


import command.RegisterCommand
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class UserController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond User.list(params), model:[userInstanceCount: User.count()]
    }

    def show(User userInstance) {
        respond userInstance
    }

    def create() {
        respond new User(params)
    }

    @Transactional
    def save(User userInstance) {
        if (userInstance == null) {
            notFound()
            return
        }

        if (userInstance.hasErrors()) {
            respond userInstance.errors, view:'create'
            return
        }

        userInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
                redirect userInstance
            }
            '*' { respond userInstance, [status: CREATED] }
        }
    }

    def edit(User userInstance) {
        respond userInstance
    }

    @Transactional
    def update(User userInstance) {
        if (userInstance == null) {
            notFound()
            return
        }

        if (userInstance.hasErrors()) {
            respond userInstance.errors, view:'edit'
            return
        }

        userInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = "用户更新成功"
                redirect userInstance
            }
            '*'{ respond userInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(User userInstance) {

        if (userInstance == null) {
            notFound()
            return
        }

        userInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'User.label', default: 'User'), userInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }

    def register()
    {
        respond new User(params)
    }

    @Transactional
    def saveUser(User userInstance,RegisterCommand cmd) {

        if (userInstance == null ) {
            notFound()
            return
        }

        if (userInstance.hasErrors() || cmd.hasErrors() || !userInstance.validate() || ! cmd.validate()) {
            /*   respond userInstance.errors, view:'register'*/
            render(view: 'register',model:[userInstance:userInstance,cmd:cmd])
            return
        }
        /*userInstance.password=userInstance.password.encodeAsPassword()*/
        userInstance.password=userInstance.password.encodeAsPassword()

        userInstance.save()
        request.withFormat {
            form multipartForm {
                flash.message = "恭喜您注册成功！"
                redirect(controller: 'goods',action:'index')
                return
            }
            render(view: 'register',model:[userInstance:userInstance,cmd:cmd])
        }
    }
    def logout(){
        session.invalidate()//清空session的内容
        redirect(uri: '/')
    }




    def search(){

        if(!params.max ) params.max=6
        if(!params.offset) params.offset=0
        def serachClosure = {
            if (params.username){
                    like('username', "%${params.username}%")
            }
        }
        def c = User.createCriteria()
        params.sort="username"
        params.order="asc"
        def userList=c.list(params,serachClosure)
        def userCount=userList.totalCount
        render(view:'index',model:[userInstanceList:userList,userInstanceCount: userCount] )

    }
}
